home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17741 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.8 KB

  1. Path: navet.enator.se!usenet
  2. From: Hardy Henneberg <hh@enator.dk>
  3. Newsgroups: comp.lang.c++,fj.lang.c++
  4. Subject: Re: [Q]When temporary object be created?
  5. Date: 17 Apr 1996 09:07:16 GMT
  6. Organization: Enator AB
  7. Message-ID: <4l2cc4$djl@navet.enator.se>
  8. NNTP-Posting-Host: 193.162.31.53
  9.  
  10. matusita@hv.epson.co.jp (Hiroshi-MATSUSHITA) writes:
  11. > When a temporary object will be created?
  12. > Suppose, I create a class named PERSON that takes a name
  13. > in its constructor like following:
  14. >   class PERSON {
  15. >     public:
  16. >       PERSON(const STRING& nm);
  17. >       ...
  18. >     private:
  19. >       STRING name;
  20. >   };
  21. > Then, when I create an instance of PERSON using raw string,
  22. > I may write like this:
  23. >   PERSON nomo("Hideo Nomo");
  24. > STRING class, in this case, is not a smart pointer, but it
  25. > creates a character buffer newly using 'new' operator in its
  26. > constructor and delete it in its destructor.
  27. > In this case, will a temporary string instance be created?
  28.  
  29. Yes, Your PERSON constructor takes a STRING as parameter.
  30. You initialize  a PERSON object with a const char*, so the compiler 
  31. to find a PERSON constructor with a const char* as parameter. Because
  32. that doesn't exits, it will use the rules for function matching to find
  33. a constructor which matches the call. One of these rules says, there 
  34. is a match if the const char* can be typeconverted into a STRING with a
  35. typeconverting function. I guess the STRING class must have a typecast 
  36. constructor (a constructor with 1 parameter), with a const char* parameter.
  37. So, this string constructor must be called resulting in a temporary STRING
  38. object.
  39.  
  40. > Does it depend on how to create the constructor?
  41. I dont' understand the question.
  42.  
  43. > Does it depend on a compiler? (undefined in C++ spec.?)
  44. No.
  45.  
  46. > If I initialize the private member 'name' using initializing
  47. > list like following, can I avoid to create a temporary object?
  48. >   PERSON::PERSON(const STRING& nm) : name(nm) {...}
  49.  
  50. No, the implementation of the PERSON constructor and the initialisation
  51. of the PERSON object will normally be in two seperate compilated modules
  52. with no possibility for the compiler to optimize.
  53.  
  54. > Why I want to know this is because I don't want to take extra time
  55. > and memory space even if it's temporary.
  56. > I'm pazzling my brain which I'd better to take between reference
  57. > approach described above and pointer approach like following:
  58. >   class PERSON {
  59. >     public:
  60. >       PERSON(const char* nm);
  61. >       ...
  62. >     private:
  63. >       STRING name;
  64. >   };
  65. > In this case (pointer approach), *no* temporary object will be
  66. > created. Also, this constructor can accept STRING object, because
  67. > STRING class has a conversion operator to "const char*", and it
  68. > works implicitly. (Does this feature bring another argue?)
  69. This is a possible solution, and you can even have both constructors.
  70. Then the function matching rules will tell the compiler to use
  71. the constructor with a const char* in your case, because it's 
  72. an exact match.
  73.  
  74. But remember, that when you define a new constructor with 1 parameter you
  75. also make a new hole in the type control, which may let errors pass
  76. the compiler.
  77.  
  78. If you are worried about memory space a solution could be
  79. to use an intelligent STRING class, which only uses one buffer for
  80. all strings with the same content.
  81.  
  82. If intialising PERSON with a const char* really is a performance issue, 
  83. I'm sure there is also another solution for that.
  84.  
  85. I think using char* all over in a program should be avoided - it's much
  86. better to use the STRING class.
  87.  
  88. I'm also against misusing type cast constructors.
  89.  
  90. My experience from working 8 years in C++ is that char* and type 
  91. cast constructors cause a lot of problems.
  92.  
  93. cheers,
  94. Hardy Henneberg
  95. ENATOR, Denmark
  96.  
  97. > (If this is a FAQ, please forgive me. Then I appreciate
  98. > if you let me know.)
  99. > Thank you,
  100. > --Hiroshi Matsushita @ Seiko Epson Corp. (matusita@hv.epson.co.jp)
  101.  
  102.